package cc.jq1024.domain.activity.service.partake;

import cc.jq1024.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import cc.jq1024.domain.activity.model.entity.*;
import cc.jq1024.domain.activity.model.valobj.UserRaffleOrderStateVO;
import cc.jq1024.domain.activity.repository.IActivityRepository;
import cc.jq1024.types.enums.ResponseCode;
import cc.jq1024.types.exception.AppException;
import cc.jq1024.types.snow.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 抽奖活动领取服务 - 抽象类的实现子类
 * @Date: 2024-06-18-19:45
 */
@Slf4j
@Service
public class RaffleActivityPartakeService extends AbstractRaffleActivityPartake {


    public RaffleActivityPartakeService(IActivityRepository repository, SnowflakeIdGenerator snowflakeIdGenerator) {
        super(repository, snowflakeIdGenerator);
    }

    /**
     * 构建用户抽奖订单
     * @param userId 用户ID
     * @param activityId 活动ID
     * @param currentDate 当前时间
     * @return 用户抽奖订单对象
     */
    @Override
    protected UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate) {
        // 1. 查询活动信息
        ActivityEntity activityEntity = repository.queryRaffleActivityByActivityId(activityId);
        // 2. 构建订单
        UserRaffleOrderEntity userRaffleOrder = new UserRaffleOrderEntity();
        userRaffleOrder.setUserId(userId);
        userRaffleOrder.setActivityId(activityId);
        userRaffleOrder.setActivityName(activityEntity.getActivityName());
        userRaffleOrder.setStrategyId(activityEntity.getStrategyId());
        userRaffleOrder.setOrderId(String.valueOf(snowflakeIdGenerator.nextId()));
        userRaffleOrder.setOrderTime(currentDate);
        userRaffleOrder.setOrderState(UserRaffleOrderStateVO.create);
        userRaffleOrder.setEndDateTime(activityEntity.getEndDateTime());
        // 3. 返回
        return userRaffleOrder;
    }

    /**
     * 用户参与抽奖活动时账户额度过滤 & 返回账户对象
     * @param userId 用户ID
     * @param activityId 活动ID
     * @param currentDate 当前时间
     * @return 账户对象
     */
    @Override
    protected CreatePartakeOrderAggregate doFilterAccount(String userId, Long activityId, Date currentDate) {
        // 1. 查询用户总额度
        ActivityAccountEntity activityAccountEntity = repository.queryActivityAccountByUserId(userId, activityId);
        // 1.1 总额度判断（只判断总剩余额度）
        if (null == activityAccountEntity || activityAccountEntity.getTotalCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_QUOTA_ERROR.getInfo());
        }

        // 2. 查询月额度
        String month = dateFormatMonth.format(currentDate);
        // 2.1 月额度判断
        ActivityAccountMonthEntity activityAccountMonthEntity = repository.queryActivityAccountMonthByUserId(userId, activityId, month);
        if (null != activityAccountMonthEntity && activityAccountMonthEntity.getMonthCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_MONTH_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_MONTH_QUOTA_ERROR.getInfo());
        }
        // 2.2 创建月账户额度；true = 存在月账户、false = 不存在月账户
        boolean isExistAccountMonth = (null != activityAccountMonthEntity);
        if (null == activityAccountMonthEntity) {
            activityAccountMonthEntity = new ActivityAccountMonthEntity();
            activityAccountMonthEntity.setUserId(userId);
            activityAccountMonthEntity.setActivityId(activityId);
            activityAccountMonthEntity.setMonth(month);
            activityAccountMonthEntity.setMonthCount(activityAccountEntity.getMonthCount());
            activityAccountMonthEntity.setMonthCountSurplus(activityAccountEntity.getMonthCountSurplus());
        }

        // 3. 查询日账户额度
        String day = dateFormatDay.format(currentDate);
        ActivityAccountDayEntity activityAccountDayEntity = repository.queryActivityAccountDayByUserId(userId, activityId, day);
        // 3.1 日额度判断
        if (null != activityAccountDayEntity && activityAccountDayEntity.getDayCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACCOUNT_DAY_QUOTA_ERROR.getCode(), ResponseCode.ACCOUNT_DAY_QUOTA_ERROR.getInfo());
        }
        // 3.2 创建日账户额度；true = 存在日账户、false = 不存在日账户
        boolean isExistAccountDay = (null != activityAccountDayEntity);
        if (null == activityAccountDayEntity) {
            activityAccountDayEntity = new ActivityAccountDayEntity();
            activityAccountDayEntity.setUserId(userId);
            activityAccountDayEntity.setActivityId(activityId);
            activityAccountDayEntity.setDay(day);
            activityAccountDayEntity.setDayCount(activityAccountEntity.getDayCount());
            activityAccountDayEntity.setDayCountSurplus(activityAccountEntity.getDayCountSurplus());
        }

        // 4. 构建对象
        CreatePartakeOrderAggregate createPartakeOrderAggregate = new CreatePartakeOrderAggregate();
        createPartakeOrderAggregate.setUserId(userId);
        createPartakeOrderAggregate.setActivityId(activityId);
        createPartakeOrderAggregate.setActivityAccountEntity(activityAccountEntity);
        createPartakeOrderAggregate.setExistAccountMonth(isExistAccountMonth);
        createPartakeOrderAggregate.setActivityAccountMonthEntity(activityAccountMonthEntity);
        createPartakeOrderAggregate.setExistAccountDay(isExistAccountDay);
        createPartakeOrderAggregate.setActivityAccountDayEntity(activityAccountDayEntity);

        // 5. 返回结果
        return createPartakeOrderAggregate;
    }
}
